package ai_auto

import (
	"gddgame.cc/galaxy/common/dsl/game"
	. "gddgame.cc/galaxy/common/dsl/game/group"
	"gddgame.cc/galaxy/utils/orm"
)

type AiData struct {
	// command不会修改，保持引用即可
	AiGroupConfigs map[int]*AiGroupConfig
	// command不会修改，保持引用即可
	Commands map[int]*AiCommand
	// condition不会修改，保持引用即可
	Conditions     map[int]*AiConditionTemplate
	ConditionGroup map[int][]int

	commandModel           *orm.Model
	conditionTemplateModel *orm.Model
	groupConfigModel       *orm.Model
}

func GetAiData(env *game.Environment) (*AiData, error) {
	var err error
	data := &AiData{}

	if data.commandModel, _, data.Commands, err = getAiCommandData(env); err != nil {
		return nil, err
	}
	if data.conditionTemplateModel, _, data.Conditions, err = getAiConditionTemplateData(env); err != nil {
		return nil, err
	}

	if data.groupConfigModel, _, data.AiGroupConfigs, err = getAiGroupConfigData(env); err != nil {
		return nil, err
	}

	// 划分条件分组
	data.ConditionGroup = make(map[int][]int)
	data.ConditionGroup[DefaultGroup] = make([]int, 0)
	for k := range data.Conditions {
		condition := data.Conditions[k]
		for _, group := range condition.Group {
			if _, ok := data.ConditionGroup[group]; !ok {
				data.ConditionGroup[group] = make([]int, 0)
			}
			data.ConditionGroup[group] = append(data.ConditionGroup[group], condition.Id)
		}
		data.ConditionGroup[DefaultGroup] = append(data.ConditionGroup[DefaultGroup], condition.Id)
	}

	return data, nil
}

func getAiCommandData(env *game.Environment) (*orm.Model, []AiCommand, map[int]*AiCommand, error) {
	var err error

	model, err := env.GetModel(AiCommandName)
	if err != nil {
		return nil, nil, nil, err
	}

	var aiCommands []AiCommand
	_, err = model.Query().Find(&aiCommands)
	if err != nil {
		return nil, nil, nil, err
	}
	Commands := make(map[int]*AiCommand, len(aiCommands))
	for k := range aiCommands {
		Commands[aiCommands[k].Id] = &aiCommands[k]
	}
	return model, aiCommands, Commands, nil
}

func getAiConditionTemplateData(env *game.Environment) (*orm.Model, []AiConditionTemplate, map[int]*AiConditionTemplate, error) {
	var err error
	model, err := env.GetModel(AiConditionTemplateName)
	if err != nil {
		return nil, nil, nil, err
	}
	var aiConditionTemplates []AiConditionTemplate
	_, err = model.Query().Find(&aiConditionTemplates)
	if err != nil {
		return nil, nil, nil, err
	}

	ConditionTemplates := make(map[int]*AiConditionTemplate, len(aiConditionTemplates))
	for k := range aiConditionTemplates {
		ConditionTemplates[aiConditionTemplates[k].Id] = &aiConditionTemplates[k]
	}
	return model, aiConditionTemplates, ConditionTemplates, nil
}

func getAiGroupConfigData(env *game.Environment) (*orm.Model, []AiGroupConfig, map[int]*AiGroupConfig, error) {
	var err error

	model, err := env.GetModel(AiGroupConfigName)
	if err != nil {
		return nil, nil, nil, err
	}

	var aiGroupConfigs []AiGroupConfig
	_, err = model.Query().Find(&aiGroupConfigs)
	if err != nil {
		return nil, nil, nil, err
	}
	AiGroupConfigs := make(map[int]*AiGroupConfig, len(aiGroupConfigs))
	for k := range aiGroupConfigs {
		AiGroupConfigs[aiGroupConfigs[k].Id] = &aiGroupConfigs[k]
	}
	return model, aiGroupConfigs, AiGroupConfigs, nil
}
